home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 20 / 2 / DISK2028.ZIP / TILES2C.C < prev    next >
C/C++ Source or Header  |  1989-01-21  |  5KB  |  154 lines

  1. /* Program to convert TILES to array initialization data in the Microsoft C  */
  2. /* language                                                                  */
  3.  
  4. /* Coded in Microsoft C 5.1 by Daniel Ross, starting 11/2/87                 */
  5. /* Revised 1/21/89    09:20                                                  */
  6.  
  7. /* The input data to this program, TILES, was created by Daniel Ross through */
  8. /* the years starting in 1959.                                               */
  9.  
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. #define FALSE           0
  15. #define TRUE            1
  16. #define TorF            int
  17.  
  18. #define MaxColsPerChar  32
  19. #define RowsPerChar     21              /* 21 rows above base line           */
  20.  
  21. FILE   * InputFile, * OutputFile ;
  22. char   InputFileName  []   =   "TILES.TXT" ;
  23. char   OutputFileName []   =   "TILES.C" ;
  24.  
  25. char   Buf [MaxColsPerChar + 4] ;
  26. char   InputChar ;
  27.  
  28. int    CharIndex, Row, Col, ActualColsThisChar ;
  29. long   Acc ;
  30. TorF   ActualColsKnown ;
  31. char   * pFinalQuote ;
  32.  
  33. void   FormatErr (void)
  34.   {
  35.     printf
  36.       ("\nFormat err, char %02x(hex), row %d up from bottom", CharIndex, Row) ;
  37.     printf ("\nActual line read:  %s", Buf) ;
  38.     exit (1) ;
  39.   }
  40.  
  41. void   main (void)
  42.   {
  43.     if ( ! (InputFile = fopen (InputFileName, "rt")))
  44.       {
  45.         printf ("\nUnable to open input file %s", InputFileName) ;
  46.         exit (1) ;
  47.       } ;
  48.     if ( ! (OutputFile = fopen (OutputFileName, "wt")))
  49.       {
  50.         printf ("\nUnable to open output file %s", OutputFileName) ;
  51.         exit (1) ;
  52.       } ;
  53.     for (CharIndex = 0 ;   CharIndex != '!' ;   ++ CharIndex)
  54.       {
  55.         fprintf (OutputFile, "        {") ;
  56.         for (Row = RowsPerChar - 1 ;   Row ;   -- Row)
  57.           fprintf (OutputFile, "0,") ;
  58.         fprintf (OutputFile, "0},\n") ;
  59.       } ;
  60.     for ( ;   CharIndex != 128 ;   ++ CharIndex)
  61.       {
  62.         if ( ! fgets (Buf, MaxColsPerChar + 4, InputFile))
  63.           {
  64.             printf
  65.               ("\nRead err or early end, at separator before char %02x(hex)",
  66.                 CharIndex) ;
  67.             exit (1) ;
  68.           } ;
  69.         if (strcmp (Buf, "--------------------\n"))
  70.           {
  71.             printf
  72.             ("\nExpecting separator before char %02x(hex), actually read:  %s",
  73.                 CharIndex, Buf) ;
  74.             exit (1) ;
  75.           } ;
  76.         if ( ! fgets (Buf, MaxColsPerChar + 4, InputFile))
  77.           {
  78.             printf
  79.               ("\nRead err or early end, at blank line before char %02x(hex)",
  80.                 CharIndex) ;
  81.             exit (1) ;
  82.           } ;
  83.         fprintf (OutputFile, "        {") ;
  84.         ActualColsKnown = FALSE ;
  85.         for (Row = RowsPerChar - 1 ;   Row >= 0 ;   -- Row)
  86.           {
  87.             if ((Row % 6)   ==   5)     fprintf (OutputFile, "\n         ") ;
  88.             if ( ! fgets (Buf, MaxColsPerChar + 4, InputFile))
  89.               {
  90.                 printf
  91.    ("\nRead err or early end, char %02x(hex), row %d up from bottom descender",
  92.                     CharIndex, Row) ;
  93.                 exit (1) ;
  94.               } ;
  95.             if ( ! ActualColsKnown)
  96.               {
  97.                 ActualColsKnown = TRUE ;
  98.                 if ( ! (pFinalQuote = strchr ( & Buf [1], '"')))   FormatErr();
  99.                                         /* Assignment, not comparison        */
  100.                 ActualColsThisChar   =   (pFinalQuote - & Buf [1]) ;
  101.                 if (ActualColsThisChar   >   MaxColsPerChar)       FormatErr();
  102.               } ;
  103.             if
  104.               (
  105.                 (Buf [0]   !=   '"')
  106.                   ||
  107.                 (( * pFinalQuote)         !=   '"')
  108.                   ||
  109.                 (( * (pFinalQuote + 1))   !=   '\n')
  110.               )
  111.               FormatErr () ;
  112.             Acc = 0 ;
  113.             for (Col = 1 ;   Col <= ActualColsThisChar ;   ++ Col)
  114.               {
  115.                 InputChar   =   Buf [Col] ;
  116.                 if (InputChar   ==   '*')
  117.                   Acc   |=   (long) (((long) 1) << (32 - Col)) ;
  118.                 else
  119.                   {
  120.                     if (InputChar   !=   ' ')     FormatErr () ;
  121.                   } ;
  122.               } ;
  123.             fprintf (OutputFile, "0x%08lx", Acc) ;
  124.             if (Row)   fprintf (OutputFile, ",") ;
  125.             else
  126.               {
  127.                 fprintf (OutputFile, "}") ;
  128.                 if (CharIndex != 127)   fprintf (OutputFile, ",") ;
  129.                 fprintf (OutputFile, "\n") ;
  130.               } ;
  131.           } ;
  132.         if ( ! fgets (Buf, MaxColsPerChar + 4, InputFile))
  133.           {
  134.             printf
  135.               ("\nRead err or early end, at blank line after char %02x(hex)",
  136.                 CharIndex) ;
  137.             exit (1) ;
  138.           } ;
  139.       } ;
  140.     if (fclose (OutputFile))
  141.       {
  142.         printf ("\nErr closing output file %s", OutputFileName) ;
  143.         exit (1) ;
  144.       } ;
  145.     if (fclose (InputFile))
  146.       {
  147.         printf ("\nErr closing input file %s", InputFileName) ;
  148.         exit (1) ;
  149.       } ;
  150.     exit (0) ;
  151.   }
  152.  
  153. /* End of file                                                               */
  154.